package com.study.web.controller.user;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.study.app.domain.AppUser;
import com.study.app.domain.bo.AppUserStudentBo;
import com.study.common.core.controller.AppBaseController;
import com.study.common.core.domain.R;
import com.study.common.core.page.TableDataInfo;
import com.study.common.fdfs.ImageUtil;
import com.study.app.domain.bo.AppUserBo;
import com.study.app.domain.vo.AppUserVo;
import com.study.app.service.IAppUserService;
import com.study.common.jpush.domain.PushBean;
import com.study.common.jpush.service.IMyJPushService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * App用户
 *
 * @author study
 * @date 2023-04-19
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/user")
public class AppUserController extends AppBaseController {

    private final IAppUserService iAppUserService;

    private final ImageUtil imageUtil;

    private final IMyJPushService pushService;

    private final IAppUserService appUserService;

    /**
     * 获取App用户详细信息
     *
     * @return
     */
    @SaCheckLogin
    @GetMapping("/details")
    public R<AppUserVo> getInfo() {
        return R.ok(iAppUserService.queryById(getUserId()));
    }


    /**
     * 修改App用户
     */
    @SaCheckLogin
    @PostMapping("/updateInfo")
    public R<Void> edit(@RequestBody AppUserBo bo) {
        bo.setId(getUserId());
        return toAjax(iAppUserService.updateByBo(bo));
    }


    /**
     * 用户图片上传，返回链接地址
     *
     * @param multipartFile
     * @return
     */
    @GetMapping("/getImage")
    public R<String> getImage(MultipartFile multipartFile) {
        String address = imageUtil.uploadFile(multipartFile);
        String location = imageUtil.location(address);
        R<String> ok = R.ok();
        ok.setData(location);
        ok.setMsg("操作成功");
        ok.setCode(200);
        return ok;
    }


    /**
     * 设置用户消息推送 deviceToken key
     *
     * @param deviceToken
     * @return
     */
    @SaCheckLogin
    @GetMapping("/setDeviceToken")
    public R<?> setUserDeviceToken(@NotNull String deviceToken) {
        AppUserVo userVo = iAppUserService.queryById(getUserId());
        return R.ok(iAppUserService.setUserDeviceToken(userVo.getUserCode(), deviceToken));
    }

    /**
     * 测试推送
     *
     * @return
     */
    @SaIgnore
    @PostMapping("/pustMessage")
    public R<?> putMessage(@RequestBody PushBean pushBean) {


        //消息推送
        PushBean bean = new PushBean();
        bean.setTitle(pushBean.getTitle());
        bean.setAlert(pushBean.getAlert());

        Map<String, String> parma = new HashMap<>();

        parma.put("jumpUrl", "/car/alarm-details?id=" + "1716750270207709186");

        bean.setExtras(parma);
        if (pushService.pushUserKey(bean, "1104a89793a86b5ff73")) {
            log.info("[{}],当前告警信息极光推送成功:{},{},{}", bean.getTitle(), "1104a89793a86b5ff73", bean.getExtras());
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 获取未读消息
     *
     * @return
     */
    @SaCheckLogin
    @GetMapping("/getMessage")
    public R<?> getMessage() {
        return appUserService.getNoReadMessage();
    }


}
